import frappe
from frappe.utils import flt
from erpnext.selling.doctype.sales_order.sales_order import SalesOrder,get_default_bom_item

class MySalesOrder(SalesOrder):
    @frappe.whitelist()
    def get_work_order_items(self, for_raw_material_request=0):
        '''Returns items with BOM that already do not have a linked work order'''
        items = []
        item_codes = [i.item_code for i in self.items]
        product_bundle_parents = [pb.new_item_code for pb in frappe.get_all("Product Bundle", {"new_item_code": ["in", item_codes]}, ["new_item_code"])]

        for table in [self.items, self.packed_items]:
            for i in table:
                bom = get_default_bom_item(i.item_code)
                if not bom:
                    template_item_code = frappe.db.get_value('Item', i.item_code, 'variant_of')
                    if template_item_code:
                        bom = get_default_bom_item(template_item_code)
                stock_qty = i.qty if i.doctype == 'Packed Item' else i.stock_qty
                if not for_raw_material_request:
                    total_work_order_qty = flt(frappe.db.sql('''select sum(qty) from `tabWork Order`
                        where production_item=%s and sales_order=%s and sales_order_item = %s and docstatus<2''', (i.item_code, self.name, i.name))[0][0])
                    pending_qty = stock_qty - total_work_order_qty
                else:
                    pending_qty = stock_qty

                if pending_qty and i.item_code not in product_bundle_parents:
                    items.append(dict(
                        name= i.name,
                        item_code= i.item_code,
                        description= i.description,
                        bom = bom or '',
                        warehouse = i.warehouse,
                        pending_qty = pending_qty,
                        required_qty = pending_qty if for_raw_material_request else 0,
                        sales_order_item = i.name
                    ))
                    
        return items